package cn.com.dao;

import cn.com.entity.Clazz;
import cn.com.entity.Course;
import cn.com.tool.Jdbc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project:Academic - ClaDao
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 22:41:20
 * @since 1.8
 */
@SuppressWarnings("all")
public class ClaDao {
    public void delete( String id ){
        Connection connection =null;
        PreparedStatement preparedStatement = null;

        try {
            connection = Jdbc.getConnection();
            preparedStatement = connection.prepareStatement("delete from t_class where id = '"+id+"';");
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(null,preparedStatement,connection);
        }

    }
    public boolean updateClassName(String id,String name,String oid){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        int i = 0;
        try {
            connection = Jdbc.getConnection();
            preparedStatement = connection.prepareStatement("select id from t_class where id ='"+id+"' and id != '"+oid+"';");
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return false;
            }
            preparedStatement = connection.prepareStatement("update t_class set id ='"+id+"',name = '"+name+"' where id = '"+oid+"';");
            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }
        if (i > 0)return true;
        return false;
    }
    public List<Clazz> find() {
        List<Clazz> list = new ArrayList<>();
        String sql = "SELECT * FROM `t_class`";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Jdbc.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                list.add(new Clazz(id,name));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }

        return list;
    }

    public Boolean addClass(String id, String name) {
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try {
            connection = Jdbc.getConnection();

            preparedStatement = connection.prepareStatement("select id from t_class where id ='"+id+"';");
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return false;
            }
            preparedStatement = connection.prepareStatement("insert into  t_class values ('"+id+"' , '"+name+"');");
            int i = preparedStatement.executeUpdate();
            if (i > 0) return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }
        return false;
    }

    public List<Course> findCourse() {
        List<Course> list = new ArrayList<>();
        String sql = "SELECT * FROM `t_courses`";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Jdbc.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                String id = resultSet.getString("c_id");
                String name = resultSet.getString("c_name");
                list.add(new Course(id,name));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }
        return list;
    }
    public boolean updateCourse(String id, String name, String oid) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        int i = 0;
        try {
            connection = Jdbc.getConnection();
            preparedStatement = connection.prepareStatement("select c_id from t_courses where c_id ="+id+" and c_id != '"+oid+"';");
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return false;
            }
            preparedStatement = connection.prepareStatement("update t_courses set c_id ="+id+",c_name = '"+name+"' where c_id = '"+oid+"';");
            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }
        if (i > 0)return true;
        return false;
    }
    public boolean addCourse(int id, String name) {
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try {
            connection = Jdbc.getConnection();

            preparedStatement = connection.prepareStatement("select c_id from t_courses where c_id ="+id+";");
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return false;
            }
            preparedStatement = connection.prepareStatement("insert into  t_courses values ("+id+",'"+name+"');");
            int i = preparedStatement.executeUpdate();
            if (i > 0) return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(resultSet,preparedStatement,connection);
        }
        return false;
    }
}
